﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.BLL.GatherEngine;
using WinnerCMS.Common;
using WinnerCMS.Licenses;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Shop
{
    [Access("ContenAdd,ContenEdit")]
    public partial class Add : PageBase
    {
        /// <summary>
        ///     自定义字段
        /// </summary>
        protected bool ExtendField;

        private readonly Goods model = new Goods();

        protected void Page_Load(object sender, EventArgs e)
        {
            InitEveryPost();

            if (!IsPostBack)
            {
                if (Request.QueryString["TempFlag"] == null && Request["ID"] == null)
                {
                    Response.Redirect(Request.RawUrl + "&TempFlag=" + Guid.NewGuid());
                }
                this.txtUpDateTime.Text = DateTime.Now.ToString();

                BindDropDownList();


                if (Request["ID"] != null) //修改界面
                {
                    EidtUI();
                }
                else //添加界面
                {
                    AddUI();
                }
            }
        }

        private void InitEveryPost()
        {
            if (License.CurrentEdition == Edition.免费版)
            {
                ErrClass.Redirect("该版本不支持此功能。");
            }

            if (Request["ID"] != null) //修改界面
            {
                var model = new GoodsManage().GetInfo(int.Parse(Request["ID"]));

                new FieldObject(4).LoadUI(this.T_DefinedFile, model.CategoryId, out this.ExtendField);
                this.li_KuoZhanShuXing.Visible = this.ExtendField;
            }
            else
            {
                new FieldObject(4).LoadUI(this.T_DefinedFile, Request["CategoryId"].ToInt(), out this.ExtendField);
                this.li_KuoZhanShuXing.Visible = this.ExtendField;
            }
        }

        private void BindDropDownList()
        {
            new SpecialManage().BindDDL(this.ddlSpecialID, BindOption.NoLink, "<请选择专题>", 4);

            new BrandManage().BindDDL(this.ddlBrandID, "<请选择品牌>");
        }

        protected string AutoTitle()
        {
            if (Request["ID"] != null) //修改界面
            {
                return "修改" + ModelManage.GetContentName(4);
            }
            return "添加" + ModelManage.GetContentName(4);
        }

        #region 添加界面

        /// <summary>
        ///     添加界面
        /// </summary>
        private void AddUI()
        {
            if (Session["Key"] != null)
            {
                this.txtKey.Text = Session["Key"].ToString();
            }
            this.txtTemplateFileName.Text = CategoryManage.GetInfo(this.model.CategoryId).ShowPageTemplateFileName;

            this.btnEdit.Visible = false;
        }

        #endregion

        #region 修改界面

        /// <summary>
        ///     修改界面
        /// </summary>
        private void EidtUI()
        {
            this.txtGoodsNum.Text = this.model.GoodsNum;
            this.txtBarCode.Text = this.model.BarCode;
            this.txtContentTitle.Text = this.model.ContentTitle;
            this.NtxtPriceMarket.Text = this.model.PriceMarket.ToString();
            this.NtxtPriceMember.Text = this.model.PriceMember.ToString();
            this.txtUnit.Text = this.model.Unit;
            this.txtKey.Text = this.model.Tags;
            this.txtPicUrl.Text = this.model.PicUrl;
            this.lblBigPicUrl.Text = this.model.BigPicUrl;
            this.NtxtStocks.Text = this.model.Stocks.ToString();
            this.NtxtAlarmNum.Text = this.model.AlarmNum.ToString();
            this.CC_Content.Text = this.model.Content;

            this.cbkIncludePic.Checked = this.model.IncludePic;
            this.cbkIsSlide.Checked = this.model.IsSlide;
            this.cbkAllowComment.Checked = this.model.AllowComment;
            this.cbkIsHeadline.Checked = this.model.IsHeadline;
            this.cbkIsOnTop.Checked = this.model.IsOnTop;
            this.cbkIsElite.Checked = this.model.IsElite;
            this.cbkIsChecked.Checked = this.model.IsChecked;
            //cbkIsDraft.Checked = model.IsDraft;


            this.ddlBrandID.SelectedValue = this.model.BrandId.ToString();
            this.txtStandard.Text = this.model.Standard;
            this.txtPackaging.Text = this.model.Packaging;
            this.ddlStars.SelectedValue = this.model.Stars.ToString();
            this.NtxtHits.Text = this.model.Hits.ToString();
            this.txtTemplateFileName.Text = this.model.TemplateFileName;

            this.txtBrief.Text = this.model.Brief;

            this.btnAdd.Visible = false;
            this.btnEdit.Visible = true;
            this.btnEdit.Text = "修改" + ModelManage.GetContentName(4);

            #region 自定义字段

            new FieldManage().ExtendFieldData(this.model.Id, this.T_DefinedFile);

            #endregion
        }

        #endregion

        #region 添加

        private void add()
        {
            var Fields = new List<string>();
            var Values = new List<object>();
            foreach (TableRow tr in this.T_DefinedFile.Rows)
            {
                var c = tr.Cells[1].Controls[0];
                if (c is TextBox)
                {
                    var tb = c as TextBox;
                    Fields.Add(tb.ID);
                    Values.Add(tb.Text.Trim());
                }
                if (c is DropDownList)
                {
                    var ddl = c as DropDownList;
                    Fields.Add(ddl.ID);
                    Values.Add(ddl.SelectedValue);
                }
                if (c is RadioButtonList)
                {
                    var rbl = c as RadioButtonList;
                    Fields.Add(rbl.ID);
                    Values.Add(rbl.SelectedValue);
                }
                if (c is CheckBox)
                {
                    var cb = c as CheckBox;
                    Fields.Add(cb.ID);
                    Values.Add(cb.Checked);
                }
            }

            this.model.GoodsNum = this.txtGoodsNum.Text;
            this.model.BarCode = this.txtBarCode.Text;
            this.model.ContentTitle = this.txtContentTitle.Text.Trim();
            this.model.PriceMarket = decimal.Parse(this.NtxtPriceMarket.Text);
            this.model.PriceMember = decimal.Parse(this.NtxtPriceMember.Text);
            //model.CategoryId = int.Parse(Request["CategoryId"].ToString());
            this.model.Unit = this.txtUnit.Text;
            this.model.Tags = this.txtKey.Text.Trim();
            this.model.PicUrl = this.txtPicUrl.Text.Trim();
            this.model.BigPicUrl = this.lblBigPicUrl.Text.Trim();
            this.model.Stocks = int.Parse(this.NtxtStocks.Text);
            this.model.AlarmNum = int.Parse(this.NtxtAlarmNum.Text);

            this.model.IncludePic = this.cbkIncludePic.Checked;
            this.model.IsSlide = this.cbkIsSlide.Checked;
            this.model.AllowComment = this.cbkAllowComment.Checked;
            this.model.IsHeadline = this.cbkIsHeadline.Checked;
            this.model.IsOnTop = this.cbkIsOnTop.Checked;
            this.model.IsElite = this.cbkIsElite.Checked;
            //model.IsDraft = cbkIsDraft.Checked == true ? (byte)1 : (byte)0;


            this.model.BrandId = int.Parse(this.ddlBrandID.SelectedValue);
            this.model.Standard = this.txtStandard.Text;
            this.model.Packaging = this.txtPackaging.Text;
            this.model.Brand = "";
            this.model.Stars = byte.Parse(this.ddlStars.SelectedValue);
            this.model.Editor = UserManage.Current.UserName;
            this.model.Hits = int.Parse(this.NtxtHits.Text.Trim());
            this.model.Brief = this.txtBrief.Text.Trim();
            this.model.Content = DownPic(this.CC_Content.Text, this.cbkDownPic.Checked);
            this.model.UpdateTime = DateTime.Parse(this.txtUpDateTime.Text);
            this.model.TemplateFileName = this.txtTemplateFileName.Text.Trim();

            if (Request["ID"] == null)
            {
                this.model.Id = 0;
            }
            else
            {
                this.model.Id = int.Parse(Request["ID"]);
            }
            var resu = new GoodsManage().Add(this.model, Request["TempFlag"], Fields, Values);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this);
            }
            else
            {
                var modelId = CategoryManage.GetInfo(this.model.CategoryId).ModelId;
                Response.Redirect(string.Format("../Common/SaveOK.aspx?ModelId={0}&ID={1}", modelId, this.model.Id));
            }
        }

        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            add();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            add();
        }

        #region 下载图片

        /// <summary>
        ///     下载图片
        /// </summary>
        /// <param name="strContent"></param>
        /// <param name="DownPic"></param>
        /// <returns></returns>
        private string DownPic(string strContent, bool DownPic)
        {
            if (DownPic)
            {
                return new Utilities().DownPic(strContent, DownPic, this.cbkDrawWatherMark.Checked,
                    CMSConfig.Instance().UpLoadFolder);
            }
            return strContent;
        }

        #endregion
    }
}